﻿#region

using System.Collections.Generic;
using System.ServiceModel;
using BTL.OnlineTestingSocial.Domain.TestingEngine;
using BTL.OnlineTestingSocial.Services.TestingEngine.Contract;
using BTL.OnlineTestingSocial.Services.TestingEngine.Service.BLL;
using BTL.Technology.Framework;

#endregion

namespace BTL.OnlineTestingSocial.Services.TestingEngine.Service
{
#if DEBUG

    [ServiceBehavior(IncludeExceptionDetailInFaults = true, ConcurrencyMode = ConcurrencyMode.Multiple,
        InstanceContextMode = InstanceContextMode.Single)]
#else
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
#endif
    public class TestingEngineService : ITestingEngineService
    {
        private readonly ICategoryManager _categoryManager;
        private readonly IQuestionGroupManager _questionGroupManager;
        private readonly ICommentManager _commentManager;
        private readonly ILikeManager _likeManager;

        public TestingEngineService()
            : this(ObjectFactory.GetType<ICategoryManager>(), ObjectFactory.GetType<IQuestionGroupManager>(),
            ObjectFactory.GetType<ICommentManager>(), ObjectFactory.GetType<ILikeManager>())
        {
        }

        public TestingEngineService(ICategoryManager categoryManager, IQuestionGroupManager questionGroupManager, ICommentManager commentManager,
            ILikeManager likeManager)
        {
            _categoryManager = categoryManager;
            _questionGroupManager = questionGroupManager;
            _commentManager = commentManager;
            _likeManager = likeManager;
        }

        /// <summary>
        /// Get all category
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Category> GetCategories()
        {
            Guard.MakeSureAllInstancesIsNullNot(_categoryManager);

            return _categoryManager.GetCategories();
        }

        /// <summary>
        /// Get question group for private page
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public IEnumerable<QuestionGroupItem> GetPrivateQuestionGroups(int userId, int pageSize, int pageNum)
        {
            Guard.MakeSureAllInstancesIsNullNot(_questionGroupManager);

            return _questionGroupManager.GetPrivateQuestionGroups(userId, pageSize, pageNum);
        }

        /// <summary>
        /// Get question group for public page
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public IEnumerable<QuestionGroupItem> GetPublicQuestionGroups(int userId, int pageSize, int pageNum)
        {
            Guard.MakeSureAllInstancesIsNullNot(_questionGroupManager);

            return _questionGroupManager.GetPublicQuestionGroups(userId, pageSize, pageNum);
        }

        /// <summary>
        /// Delete an array Question group by ids list
        /// </summary>
        /// <param name="ids">question group ids list</param>
        /// <returns></returns>
        public bool DeleteQuestionGroup(string ids)
        {
            Guard.MakeSureAllInstancesIsNullNot(_questionGroupManager);

            return _questionGroupManager.DeleteQuestionGroupSp(ids);
        }

        /// <summary>
        /// insert comment
        /// </summary>
        /// <param name="content"></param>
        /// <param name="questionDetailId"></param>
        /// <param name="questionGroupId"></param>
        /// <param name="createBy"></param>
        /// <returns></returns>
        public bool InsertComment(string content, int? questionDetailId, int? questionGroupId, string createBy)
        {
            Guard.MakeSureAllInstancesIsNullNot(_commentManager);

            return _commentManager.InsertComment(content, questionDetailId, questionGroupId, createBy);
        }

        /// <summary>
        /// insert like
        /// </summary>
        /// <param name="isDislike"></param>
        /// <param name="questionDetailId"></param>
        /// <param name="questionGroupId"></param>
        /// <param name="createBy"></param>
        /// <returns></returns>
        public bool InsertLike(bool isDislike, int? questionDetailId, int? questionGroupId, string createBy)
        {
            Guard.MakeSureAllInstancesIsNullNot(_likeManager);

            return _likeManager.InsertComment(isDislike, questionDetailId, questionGroupId, createBy);
        }

        public QuestionGroupItem GetQuestionGroupDetail(int questionGroupId)
        {
            Guard.MakeSureAllInstancesIsNullNot(_questionGroupManager);

            return _questionGroupManager.GetDetail(questionGroupId);
        }
    }
}